package com.qingzhuge.web.autoconfigure;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.util.List;
/**
 * @author : zero.xiao
 * @description :
 * @date :2018/7/4 0004 下午 16:32
 * @since : 1.0.0
 * @modified :
 */
@Configuration
public class MessageConverterConfiguration implements WebMvcConfigurer {

    private final FastJsonHttpMessageConverter fastJsonHttpMessageConverter;

    /**
     * string类型的解析器
     * @return 不带双引号的字符串
     */
    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter(){
        return new StringHttpMessageConverter(StandardCharsets.UTF_8);
    }

    @Autowired
    public MessageConverterConfiguration(
                                         FastJsonHttpMessageConverter fastJsonHttpMessageConverter) {
        this.fastJsonHttpMessageConverter = fastJsonHttpMessageConverter;
    }


    /**
     * 配置fastJson  用于替代jackson
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(stringHttpMessageConverter());
        converters.add(fastJsonHttpMessageConverter);
    }
}